using System;
using System.Windows;
using System.Windows.Controls;

namespace GameOverlayWPF
{
    /// <summary>
    /// UpdateDialog.xaml 的交互逻辑
    /// </summary>
    public partial class UpdateDialog : Window
    {
        private readonly VersionInfo _versionInfo;
        public bool UserWantsUpdate { get; private set; } = false;

        public UpdateDialog(VersionInfo versionInfo)
        {
            InitializeComponent();
            _versionInfo = versionInfo ?? throw new ArgumentNullException(nameof(versionInfo));
            
            // 初始化UI
            InitializeUI();
        }

        private void InitializeUI()
        {
            // 设置版本信息
            VersionNameText.Text = $"v{_versionInfo.VersionName}";
            UpdateTitleText.Text = _versionInfo.UpdateTitle;
            UpdateContentText.Text = _versionInfo.UpdateContent;

            // 根据是否强制更新调整UI
            if (_versionInfo.IsForceUpdate)
            {
                // 强制更新模式
                TitleText.Text = "强制更新";
                ForceUpdateBorder.Visibility = Visibility.Visible;
                
                // 隐藏稍后更新按钮和关闭按钮
                LaterButton.Visibility = Visibility.Collapsed;
                CloseButton.Visibility = Visibility.Collapsed;
                
                // 更新按钮占满整行
                Grid.SetColumn(UpdateButton, 0);
                Grid.SetColumnSpan(UpdateButton, 3);
            }
            else
            {
                // 非强制更新模式
                TitleText.Text = "发现新版本";
                ForceUpdateBorder.Visibility = Visibility.Collapsed;
                
                // 显示稍后更新按钮和关闭按钮
                LaterButton.Visibility = Visibility.Visible;
                CloseButton.Visibility = Visibility.Visible;
            }
        }

        private async void UpdateButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                // 记录点击统计
                await VersionUpdateService.RecordUpdateClick(_versionInfo.Id);
                
                // 打开下载链接
                VersionUpdateService.OpenDownloadUrl(_versionInfo.DownloadUrl);
                
                UserWantsUpdate = true;
                
                // 如果是强制更新，关闭应用程序
                if (_versionInfo.IsForceUpdate)
                {
                    MessageBox.Show("更新程序已打开，请下载更新后重新启动应用程序。", 
                        "更新提示", 
                        MessageBoxButton.OK, 
                        MessageBoxImage.Information);
                    
                    Application.Current.Shutdown();
                }
                else
                {
                    // 非强制更新，只关闭对话框
                    DialogResult = true;
                    Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"打开更新链接失败: {ex.Message}", 
                    "错误", 
                    MessageBoxButton.OK, 
                    MessageBoxImage.Error);
            }
        }

        private void LaterButton_Click(object sender, RoutedEventArgs e)
        {
            // 用户选择稍后更新
            UserWantsUpdate = false;
            DialogResult = false;
            Close();
        }

        private void CloseButton_Click(object sender, RoutedEventArgs e)
        {
            // 用户关闭对话框
            UserWantsUpdate = false;
            DialogResult = false;
            Close();
        }

        // 防止用户通过Alt+F4等方式关闭强制更新对话框
        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            if (_versionInfo.IsForceUpdate && !UserWantsUpdate)
            {
                e.Cancel = true;
                MessageBox.Show("此版本为强制更新，必须更新后才能继续使用。", 
                    "更新提示", 
                    MessageBoxButton.OK, 
                    MessageBoxImage.Warning);
            }
            
            base.OnClosing(e);
        }
    }
}
